using System;
using Rarous.NForms.Validation;

namespace Rarous.NForms {
  /// <summary>
  /// Třída reprezentující TextAreu.
  /// </summary>
  public class TextAreaItem : FormItem {
    /// <summary>
    /// Konstruktor.
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="name"></param>
    /// <param name="label"></param>
    /// <param name="rows"></param>
    /// <param name="cols"></param>
    public TextAreaItem(Form parent, string name, string label, int rows, int cols)
      : this(parent, name, label, rows, cols, null) {
    }
    /// <summary>
    /// Konstruktor.
    /// </summary>
    /// <param name="parent"></param>
    /// <param name="name"></param>
    /// <param name="label"></param>
    /// <param name="rows"></param>
    /// <param name="cols"></param>
    public TextAreaItem(Form parent, string name, string label, int rows, int cols, object attributes) {
      ConstructorContract(parent, name);

      Control = new HtmlElement(TextAreaTag, new { Name = name, Id = ConvertNameToId(name), Rows = rows, Cols = cols });
      Label = CreateLabel(name, label);

      Register(parent, name);
      SetAttributes(attributes);
    }

    /// <summary>
    /// Nastaví prvek jako povinný.
    /// </summary>
    /// <param name="message">Chybová hláška při nesplnění pravidla.</param>
    /// <returns></returns>
    public TextAreaItem Required(string message) {
      AddRule(new PredicateRule<string>(message, s => String.IsNullOrEmpty(s) == false));
      Control.AddClass("required");
      return this;
    }

    public override FormItem SetValue(object value) {
      Control.SetText(value.ToString());
      return this;
    }
  }
}
